﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using DataObjectsPhone;
using System.ComponentModel;


namespace ee_wp_tourmaker
{
    public partial class ChallengeBrowserPivot : PhoneApplicationPage
    {

        public ChallengeBrowserPivot()
        {
            InitializeComponent();

            App.MainModel.challengeBrowser = this;
            //fillings tables
            fillTableWithChallenges(activeChallengeListBox, App.ChallengeData.ActiveChallenges);
            fillTableWithChallenges(finishedChallengeListBox, App.ChallengeData.DoneChallenges);
            fillTableWithChallenges(searchChallengeListBox, App.ChallengeData.WebChallenges);

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            
        }

        public void MainModelPropertyLoadedWebData(object sender, PropertyChangedEventArgs e)
        {
            MessageBox.Show(e.ToString());
            fillTableWithChallenges(searchChallengeListBox, App.ChallengeData.WebChallenges);
        }


        private void ApplicationBarSearchButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/SearchChallengePage.xaml", UriKind.Relative));
        }

        private void pivotMain_LoadingPivotItem(object sender, PivotItemEventArgs e)
        {
            if (e.Item == searchPivotItem)
                ApplicationBar.IsVisible = true;
            else
                ApplicationBar.IsVisible = false;
        }

        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (((Pivot)sender).SelectedIndex)
            {
                case 0:
                    ApplicationBar.IsVisible = false;
                    break;

                case 1:
                    ApplicationBar.IsVisible = false;
                    break;

                case 2:
                    ApplicationBar.IsVisible = true;
                    break;
            }
        }

        private void ApplicationBarNewButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/NewChallengePage.xaml", UriKind.Relative));
        }


        private void fillTableWithChallenges(ListBox table, List<Challenge> objects)
        {
            //bugy????
            table.Items.Clear();

            if (objects == null && table == searchChallengeListBox)
            {
                Challenge c = new Challenge();
                c.Name = "Downloading";
                ChallengeCell emptycell = new ChallengeCell();
                emptycell.setChallenge(c);
                table.Items.Add(emptycell);

            }
            else
            {
                for (int i = 0; i < objects.Count; i++)
                {
                    ChallengeCell cell = new ChallengeCell();
                    cell.setChallenge(objects[i]);
                    table.Items.Add(cell);
                }
            }
        }

        public void refreshWebList()
        {
            fillTableWithChallenges(searchChallengeListBox, App.ChallengeData.WebChallenges);
        }

        private void searchChallengeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (searchChallengeListBox.SelectedIndex != -1)
            {
                int index = (sender as ListBox).SelectedIndex;
                if (App.ChallengeData.WebChallenges.ElementAt(index) != null)
                {
                    Challenge selectedChallenge = App.ChallengeData.WebChallenges.ElementAt(index);

                    App.ChallengeData.downloadDetailedForid(selectedChallenge.Id);

                    App.MainModel.SelectedChallenge = selectedChallenge;
                    NavigationService.Navigate(new Uri("/Views/ChallengePivot.xaml", UriKind.Relative));


                    searchChallengeListBox.SelectedIndex = -1;
                }
           }
        }

        private void finishedChallengeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (finishedChallengeListBox.SelectedIndex != -1)
            {
                int index = (sender as ListBox).SelectedIndex;

                Challenge selectedChallenge = App.ChallengeData.DoneChallenges.ElementAt(index);
                App.MainModel.SelectedChallenge = selectedChallenge;
                NavigationService.Navigate(new Uri("/Views/ChallengePivot.xaml", UriKind.Relative));

                finishedChallengeListBox.SelectedIndex = -1;
            }
        }

        private void activeChallengeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (activeChallengeListBox.SelectedIndex != -1)
            {
                int index = (sender as ListBox).SelectedIndex;

                Challenge selectedChallenge = App.ChallengeData.ActiveChallenges.ElementAt(index);
                App.MainModel.SelectedChallenge = selectedChallenge;
                NavigationService.Navigate(new Uri("/Views/ChallengePivot.xaml", UriKind.Relative));

                activeChallengeListBox.SelectedIndex = -1;
            }

        }

        
    }
}
